package com.zy.mc_module.ui.setting.setpay;

import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.zy.mc_module.R;
import com.zy.mc_module.base.BaseActivity;
import com.zy.mc_module.common.RxBusCode;
import com.zy.mc_module.utils.StatusBarUtil;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.utils.UserUtils;
import com.zy.mc_module.view.CustomHeadLayout;
import com.zy.mc_module.view.PaymentView;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import butterknife.BindView;

/**
 * Created on 2019/04/14
 * 支付设置密码
 */
public class SetPayActivity extends BaseActivity<SetPayContract.View, SetPayPresenter> implements SetPayContract.View {

    @BindView(R.id.customhead)
    CustomHeadLayout customhead;
    @BindView(R.id.setpay_payview)
    PaymentView payview;
    @BindView(R.id.setpay_hint)
    TextView tv_hint;

    private String title;
    private boolean isFlag;//true:设置密码/false修改密码
    private int time = 0;//密码输入完成的次数
    private String pwd;//密码

    @Override
    protected SetPayPresenter createPresenter() {
        return new SetPayPresenter();
    }

    @Override
    protected boolean isNeedRxBus() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setpay;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setImmersiveStatusBar(this);
        title = getIntent().getStringExtra("title");
        customhead.setText(title);
        customhead.setLeftClickListener(view -> finish());
        if (title.contains("设置")) {
            isFlag = true;
            tv_hint.setText("请输入6位支付密码");
        } else {
            isFlag = false;
            tv_hint.setText("验证原6位数字支付密码");
        }
        //支付密码输入框监听事件
        payview.setOnFinishInput((password, isAuto) -> {
            if (isFlag) {
                //-----设置密码-----
                setPwd(password);
            } else {
                //-----修改密码-----
                changePwd(password);
            }
        });
    }

    /***
     * 设置密码
     */
    private void setPwd(String password) {
        time++;
        if (time >= 2) {
            //第二次 是来验证和第一次输入的密码是否一样
            if (pwd.equals(password)) {
                if (getIntent().hasExtra("code")) {
                    //找回支付密码
                    presenter.forgetPayPwd(context, getIntent().getStringExtra("code"), password);
                } else {
                    //初次设置密码
                    presenter.setPayPwd(context, password);
                }
            } else {
                time = 0;//清空次数
                payview.clearPwd();
                tv_hint.setText("验证原6位数字支付密码");
                ToastUtil.showShort("两次输入的密码不一致，请重试");
            }
        } else if (time == 1) {
            pwd = password;
            payview.clearPwd();
            tv_hint.setText("请再次输入，以确认密码");
        }
    }

    /***
     * 修改密码
     */
    private void changePwd(String password) {
        time++;
        if (time >= 2) {
            //第二次 输入新密码
            presenter.changePayPwd(context, pwd, password);
        } else if (time == 1) {
            //校验原密码
            pwd = password;
            presenter.checkPayPwd(context, password);
        }
    }

    /***
     * 设置支付密码成功
     */
    @Override
    public void onSuccess(boolean isSuc, String msg) {
        if (isSuc) {
            finish();//成功
            ToastUtil.showShort("设置成功");
        } else {
            ToastUtil.showShort(msg);
        }
    }

    /***
     * 校验密码
     */
    @Override
    public void checkSuccess(boolean isSuc, String msg) {
        if (isSuc) {
            //原密码正确
            tv_hint.setText("请输入新的6位支付密码");
        } else {
            time = 0;
            ToastUtil.showShort(msg);
        }
        payview.clearPwd();
    }

    /***
     * 找回支付密码
     */
    @Override
    public void forgetSuccess(boolean isSuc) {
        if (isSuc) {
            finish();
            RxBus.get().post(RxBusCode.DIS_CODE, "");
            ToastUtil.showShort("找回支付密码成功");
        } else {
            ToastUtil.showShort("找回支付密码失败");
        }
    }

    @Override
    public void onError(ThdException error) {
        ToastUtil.showShort(error.message);
    }
}
